//
//  FriendlinNewMessageViewController.m
//  CheekiOS
//
//  Created by mac on 2017/6/9.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "FriendlinNewMessageViewController.h"
#import "FriendlinNewCell.h"
#import "SDContactModel.h"
#import "FriendNewDetailsViewController.h"
#import "MessageModelManager.h"

@interface FriendlinNewMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) FriendlinNewModel *Model;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger rows;
@property (nonatomic, strong) UILabel *historyLab;

@end

@implementation FriendlinNewMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName : FontP(18)};
    self.navigationController.navigationBar.barTintColor = Colorbackground;
    [self.navigationController.navigationBar setTranslucent:NO];

    _page = 1;
    _rows = 10;
    _tableView = [[UITableView alloc] init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewStylePlain;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight-64);
    
    [self.view addSubview:_tableView];
    
    UIButton *screeningBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [screeningBtn addTarget:self action:@selector(screeningBtnActionClick) forControlEvents:UIControlEventTouchUpInside];
    
    [screeningBtn setTitle:@"清空" forState:UIControlStateNormal];
    screeningBtn.titleLabel.font = FontP(17);
    [screeningBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    screeningBtn.frame = CGRectMake(0, 0, 44, 44);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc ] initWithCustomView:screeningBtn];
    self.navigationItem.rightBarButtonItem = backItem;
    
    
    
    [self requestdata];
    
}

- (void)screeningBtnActionClick{

    [HcardHttpmanager getFrienduserCircleEmpty:[HQHttpManager getUserId] success:^(id result) {
        if([result[@"code"] isEqualToString:@"ok"]){
            [_dataSource removeAllObjects];
            [_tableView reloadData];

        }
    } failure:^(NSError *error) {
        
    }];
    
    
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

#pragma mark ==============tableViewdelegate  datasource=============

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _dataSource.count;

    }else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 87;
    }else{
        return 87;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        FriendlinNewCell *cell = [FriendlinNewCell cellWithTableView:tableView indexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        _Model = _dataSource[indexPath.row];
        cell.model = _Model;
        
        return cell;
    }else{
        static NSString *str = @"historyCellident";
     
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            
            _historyLab = [[UILabel alloc] init];
            _historyLab.text = @"查看历史消息...";
            _historyLab.font = FontP(18);
            _historyLab.textAlignment = NSTextAlignmentCenter;
            _historyLab.textColor =RGB(166, 167, 170);
            _historyLab.frame = CGRectMake(0, 0, KScreenWidth, 86);
            
            UILabel *lin = [[UILabel alloc] initWithFrame:CGRectMake(0, _historyLab.bottom, KScreenWidth, 1)];
            lin.backgroundColor = TheLinColor;
            
            [cell.contentView addSubview:_historyLab];
            [cell.contentView addSubview:lin];
        }
        

        
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        [self requesthistorydata];
    }else{
        _Model = _dataSource[indexPath.row];
        FriendNewDetailsViewController *NewVc = [[FriendNewDetailsViewController alloc] init];
        NewVc.navigationController.navigationBar.hidden = YES;

        NewVc.CircleId = _Model.circleId;
        [self.navigationController pushViewController:NewVc animated:YES];
    
    }
}

#pragma mark ============数据请求============
-(void)requestdata{
    
    [HcardHttpmanager FriendNewContentUserId:[HQHttpManager getUserId] successBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        
        _dataSource =[FriendlinNewModel mj_objectArrayWithKeyValuesArray:resultDic[@"res"]];
        
        NSLog(@"=======%@",resultDic);
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
        
    }];
    
}

- (void)requesthistorydata{
    
    
    [HcardHttpmanager FriendNewhistoryUserId:[HQHttpManager getUserId] page:_page rows:_rows successBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        NSLog(@"%ld",(long)_page);
        NSMutableArray *arr = [FriendlinNewModel mj_objectArrayWithKeyValuesArray:resultDic[@"res"]];
 
        [_dataSource addObjectsFromArray:arr];
        
        if (arr.count == 0) {
            _historyLab.text = @"没有更多的历史消息了";
        }
//        NSLog(@"+++++++========++++++++++======+++++%@",_dataSource);
        _page++;
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        _page--;
    }];
}


@end
